import java.util.StringTokenizer;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;

/**
 * Created by IntelliJ IDEA.
 * User: christopher_moore
 * Date: Jul 24, 2007
 * Time: 2:25:20 PM
 * To change this template use File | Settings | File Templates.
 */
public class Item {
    private String itemKey;
    private String itemName;
    private int itemQuality;
    private int itemMinLevel;
    private int auctionItemClass;
    private int auctionItemSubClass;
    private int inventoryType;
    private int texture;

    public Item(String itemKey, String itemName, int itemQuality, int itemMinLevel, int auctionItemClass,
                int auctionItemSubClass, int inventoryType, int texture) {
        this.itemKey = itemKey;
        this.itemName = itemName;
        this.itemQuality = itemQuality;
        this.itemMinLevel = itemMinLevel;
        this.auctionItemClass = auctionItemClass;
        this.auctionItemSubClass = auctionItemSubClass;
        this.inventoryType = inventoryType;
        this.texture = texture;
    }

    public Item(String line) {
        //
        // Sample:  ["15124:760:0"] = "Robust Cloak of the Owl;2;20;7;14;1;10;331",
        //
        int delim1;
        int delim2;

        // Parse out the itemKey
        delim1 = line.indexOf("[\"");
        delim2 = line.indexOf("\"]");
        String itemKeyString = line.substring(delim1+2, delim2);
        this.itemKey = new String(itemKeyString);

        // Extract the right-side
        delim1 = line.indexOf(" \"");
        delim2 = line.indexOf("\",");
        String rightSide = line.substring(delim1+2, delim2);

        StringTokenizer st = new StringTokenizer(rightSide, ";");

        this.itemName = st.nextToken();
        if (this.itemName.contains("'")) {
            this.itemName = this.itemName.replace("'", "''");
        }
        this.itemQuality = Integer.parseInt(st.nextToken());
        this.itemMinLevel = Integer.parseInt(st.nextToken());
        this.auctionItemClass = Integer.parseInt(st.nextToken());
        this.auctionItemSubClass = Integer.parseInt(st.nextToken());
        this.inventoryType = Integer.parseInt(st.nextToken());
        this.texture = Integer.parseInt(st.nextToken());



    }

    public String getItemKey() {
        return itemKey;
    }

    public String getItemName() {
        return itemName;
    }

    public void write(Connection theDb) {
        String sqlText;
        Statement sql;

        try {
            sql = theDb.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Failed to create SQL statement");
            return;
        }

        sqlText = "insert into items (object_id, item_name, item_quality, item_min_level, item_class, item_sub_class, " +
                "item_inventory_type, item_texture) values ("
                + "'" + itemKey + "', "
                + "'" + itemName + "', "
                + itemQuality + ", "
                + itemMinLevel + ", "
                + auctionItemClass + ", "
                + auctionItemSubClass + ", "
                + inventoryType + ", "
                + texture
                + ")";

        try {
            sql.executeUpdate(sqlText);
        } catch (SQLException e) {
            if (e.getMessage().startsWith("ERROR: duplicate key")) {
                // Ignore duplicate key errors
            } else {
                e.printStackTrace();
                System.out.println("Update failed");
            }
        }
    }

}
